/**
 * 权限管理初始化
 */
var Permission = {
    id: "permissionTable", //表格id
    seItem: null, //选中的条目
    table: null,
    layerIndex: -1,
    appTree: null
};

/**
 * 初始化表格的列
 */
Permission.initColumn = function () {
    var columns = [
        {field: 'selectItem', radio: true},
        {title: 'id', field: 'id', visible: false, align: 'center', valign: 'middle', width: '50px'},
        {title: '应用名称', field: 'appName', align: 'center', valign: 'middle', width: '180px'},
        {title: '菜单名称', field: 'name', align: 'center', valign: 'middle'},
        {title: '请求地址', field: 'url', align: 'center', valign: 'middle', width: '150px'},
        {title: '是否是菜单', field: 'isMenuName', align: 'center', valign: 'middle', width: '150px'},
        {title: '图标', field: 'icon', align: 'center', valign: 'middle', width: '150px'},
        {title: '排序', field: 'num', align: 'center', valign: 'middle', width: '50px'},
        {title: '创建时间', field: 'gmtCreate', align: 'center', valign: 'middle', width: '140px'},
        {title: '更新时间', field: 'gmtModified', align: 'center', valign: 'middle', width: '140px'}
    ];
    return columns;
};

/**
 * 检查是否选中
 */
Permission.check = function () {
    var selected = $('#' + this.id).bootstrapTreeTable('getSelections');
    if(selected.length == 0){
        Common.info("请先选中表格中的某一记录！");
        return false;
    }else{
        Permission.seItem = selected[0];
        return true;
    }
};

/**
 * 点击添加权限
 */
Permission.openAddPermission = function () {
    var index = layer.open({
        type: 2,
        title: '添加权限',
        area: ['800px', '520px'], //宽高
        fix: false, //不固定
        maxmin: true,
        content: Common.ctxPath + '/permission/to_add'
    });
    this.layerIndex = index;
};

/**
 * 点击编辑权限
 */
Permission.openEditPermission = function () {
    if (this.check()) {
        var permissionId = this.seItem.id;
        var index = layer.open({
            type: 2,
            title: '编辑权限',
            area: ['800px', '520px'], //宽高
            fix: false, //不固定
            maxmin: true,
            content: Common.ctxPath +'/permission/to_update/' + permissionId
        });
        this.layerIndex = index;
    }
};

/**
 * 删除权限
 */
Permission.deletePermission = function () {
    if (this.check()) {
        var permissionId = this.seItem.id;
        var operation = function(){
            var ajax = new $ax(Common.ctxPath + "/permission/remove", function (result) {
                if (result.success) {
                    Common.success(result.message + "!");
                    Permission.table.refresh();
                } else {
                    Common.error(result.message + "!");
                }
            }, function (result) {
                Common.error(result.responseJSON.message + "!");
            });
            ajax.set("permissionId", permissionId);
            ajax.start();
        };
        Common.confirm("是否删除该权限以及所有子权限？", operation);
    }
};

/**
 * 重置查询
 */
Permission.resetSearch = function () {
    $("#condition").val("");
    Permission.search();
}

/**
 * 查询权限列表
 */
Permission.search = function () {
    var queryData = {};
    queryData['appId'] = $("#appId").val();
    queryData['condition'] = $("#condition").val();
    Permission.table.refresh({query: queryData});
};

/**
 * 点击应用input框时
 */
Permission.onClickApp = function (e, treeId, treeNode) {
    $("#appName").val(Permission.appTree.getSelectedVal());
    $("#appId").val(treeNode.id);
};

/**
 * 双击应用input框
 */
Permission.onDblClickApp = function (e, treeId, treeNode) {
    $("#appName").val(Permission.appTree.getSelectedVal());
    $("#appId").val(treeNode.id);
    $("#appContent").fadeOut("fast");
};

/**
 * 显示应用选择的树
 */
Permission.showAppSelectTree = function () {
    var inputDiv = $("#appName");
    var inputDivOffset = inputDiv.position();
    var inputParentDivOffset = inputDiv.parent().position();
    Common.showInputTree("appName", "appContent", inputParentDivOffset.left + inputDivOffset.left, inputDiv.outerHeight());
};

$(function () {
    var defaultColunms = Permission.initColumn();
    var table = new BSTreeTable(Permission.id, "/permission/list", defaultColunms);
    table.setExpandColumn(3);
    table.setIdField("id");
    table.setCodeField("id");
    table.setParentCodeField("parentId");
    table.setExpandAll(true);
    table.init();
    Permission.table = table;
    var appTree = new $ZTree("appTree", "/tree/app");
    appTree.bindOnClick(Permission.onClickApp);
    appTree.bindOnDblClick(Permission.onDblClickApp)
    appTree.init();
    Permission.appTree = appTree;
});
